<template>
  <div class="users mt-15">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索部分 -->
    <div class="user_search mt-15 mb-15">
      <!-- 通过动态绑定获取搜索内容 -->
      <el-input placeholder="请输入内容" v-model="searchVal" class="search_input">
        <!-- 点击搜索获取搜索内容 -->
        <el-button slot="append" icon="el-icon-search" @click="handleSearch"></el-button>
      </el-input>
      <el-button type="primary" plain @click="handleAdd">添加用户</el-button>
    </div>
    <!-- 添加用户对话框 -->
    <!-- 监听handleDialogClose事件，重置校验信息 -->
    <el-dialog title="添加用户" :visible.sync="addDialogFormVisible" @close="handleDialogClose">
      <!-- 在el-form组件添加rules属性 -->
      <el-form :model="addForm" :rules="verifyRules" ref="addRef">
        <el-form-item label="用户名称" label-width="90px" prop="username">
          <el-input v-model="addForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" label-width="90px" prop="password">
          <el-input v-model="addForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" label-width="90px" prop="email">
          <el-input v-model="addForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户电话" label-width="90px" prop="mobile">
          <el-input v-model="addForm.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 表格 -->
    <div class="user_table mt-15 mb-15">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" width="50">#</el-table-column>
        <el-table-column prop="username" label="姓名" width="180"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column label="状态"></el-table-column>
        <el-table-column label="操作">
          <!-- 操作部分 -->
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
              type="primary"
              icon="el-icon-edit"
              circle size="mini"
              @click="handleEdit(scope.row)">
              </el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                size="mini"
                @click="handleDelete(scope.row)"
              ></el-button>
              <el-button type="success" icon="el-icon-check" circle size="mini"></el-button>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑页面 -->
    <el-dialog title="编辑用户" :visible.sync="editDialogFormVisible">
      <!-- 在el-form组件添加rules属性 -->
      <el-form :model="editForm" :rules="editRules" ref="editRef">
        <el-form-item label="用户名称" label-width="90px">
          <el-input v-model="editForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" label-width="90px" prop="email">
          <el-input v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户电话" label-width="90px" prop="mobile">
          <el-input v-model="editForm.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmEdit">确 定</el-button>
      </div>

    <!-- 页脚分页 -->
    <el-pagination
      class="page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="400"
    ></el-pagination>
  </div>
</template>
<script>
import { getUserList, addUser, delUser } from '@/api'
export default {
  created () {
    this.initUser()
  },
  methods: {
    initUser () {
      getUserList({ query: this.searchVal, pagenum: '1', pagesize: 10 }).then(
        res => {
          console.log(res)
          if (res.data.meta.status === 200) {
            this.tableData = res.data.data.users
          }
        }
      )
    },
    // 搜索部分
    handleSearch () {
      this.initUser()
    },
    // 弹窗添加用户
    handleAdd () {
      this.addDialogFormVisible = true
    },
    handleEdit (row) {
      this.editDialogFormVisible = true
    },
    // 关闭添加用户对话框
    handleDialogClose () {
      // 清除校验信息需要调用form表单的clearValidate重置方法
      this.$refs.addRef.clearValidate()
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
    },
    // 操作部分
    // 删除用户
    handleDelete (row) {
      // console.log(row.id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点确定后执行发送删除请求
        delUser(row.id)
          .then(res => {
            if (res.data.meta.status === 200) {
              this.$message.success(res.data.meta.msg)
              this.initUser()
            } else {
              this.$message.error(res.data.meta.msg)
            }
          })
          // 点取消则弹框提示
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
    },
    // 添加用户请求
    confirmAdd () {
      this.$refs.addRef.validate(isPass => {
        if (isPass) {
          // 发请求
          addUser(this.addForm).then(res => {
            if (res.data.meta.status === 201) {
              // 关闭添加用户窗口
              this.addDialogFormVisible = false
              // 刷新数据
              this.initUser()
              this.addForm = {
                username: '',
                password: '',
                email: '',
                mobile: ''
              }
              // 弹出提示
              this.$message.success(res.data.meta.msg)
            } else {
              this.$message.error(res.data.meta.msg)
            }
          })
        } else {
          this.$message.error('请输入正确的信息')
        }
      })
    }
  },
  data () {
    let checkEmail = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入邮箱'))
      } else {
        let emailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
        if (emailReg.test(value)) {
          callback()
        } else {
          callback(new Error('请输入正确的邮箱'))
        }
      }
    }
    return {
      searchVal: '', // 搜索内容
      tableData: [], // 用户列表
      currentPage4: 4,
      addDialogFormVisible: false, // 控制添加用户弹出对话框
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 添加用户校验信息
      verifyRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        email: [{ validator: checkEmail, trigger: ['blue', 'change'] }],
        mobile: [{ required: true, message: '手机不能为空', trigger: 'blur' }]
      },
      // 编辑对话框
      editDialogFormVisible: false,
      editForm: {
        username: '',
        email: '',
        mobile: ''
      },
      editRules: {
        email: [{ validator: checkEmail, trigger: ['blue', 'change'] }],
        mobile: [{ required: true, message: '手机不能为空', trigger: 'blur' }]
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.users {
  color: red;
}
.search_input {
  width: 300px;
  margin-right: 20px;
}
</style>
